package com.thingcom.delinrobot.main.Launch.NewUser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import com.thingcom.delinrobot.Common.Constant;
import com.thingcom.delinrobot.R;
import com.thingcom.delinrobot.base.BaseBackFragment;
import com.thingcom.delinrobot.utils.PreferencesUtil;
import com.thingcom.delinrobot.view.SimpleToolbar;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/11/27
 */
public class ChooseUserAreaBackFragment extends BaseBackFragment {

    @BindView(R.id.toolbar_user_area)
    SimpleToolbar toolbarUserArea;
    @BindView(R.id.tv_user_area_label)
    TextView tvUserAreaLabel;
    @BindView(R.id.tv_user_area_choose)
    TextView tvUserAreaChoose;
    @BindView(R.id.cl_user_area_choose)
    ConstraintLayout clUserAreaChoose;
    @BindView(R.id.btn_user_area_continue)
    Button btnUserAreaContinue;
    private String UserArea;

    public static ChooseUserAreaBackFragment newInstance(String country) {

        Bundle args = new Bundle();
        args.putString(Constant.CHOOSE_COUNTRY,country);
        ChooseUserAreaBackFragment fragment = new ChooseUserAreaBackFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_back_choose_user_area, container, false);
        unbinder = ButterKnife.bind(this, view);
        initToolbar(toolbarUserArea);
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle!=null){
            UserArea = bundle.getString(Constant.CHOOSE_COUNTRY);

        }

        super.onCreate(savedInstanceState);
    }

    @OnClick({R.id.cl_user_area_choose, R.id.btn_user_area_continue})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cl_user_area_choose:
                ChooseCountryBackFragment fragment = ChooseCountryBackFragment.newInstance();
                fragment.setTargetFragment(ChooseUserAreaBackFragment.this,Constant.SET_CHOOSE_COUNTRY );
                start(fragment);
                break;
            case R.id.btn_user_area_continue:
                start(RegisterMailFragment.newInstance());
                PreferencesUtil.getInstance(getContext()).putString(Constant.CHOOSE_COUNTRY,UserArea);
                break;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == Constant.SET_CHOOSE_COUNTRY && resultCode == Activity.RESULT_OK){
            if (data!=null){
                Log.i("地区", "onActivityResult: "+"Country"+Constant.SET_CHOOSE_COUNTRY);
                UserArea = data.getStringExtra(Constant.CHOOSE_COUNTRY);
                tvUserAreaChoose.setText(UserArea);
            }
        }
    }
}
